<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.sn" placeholder="请输入部门编号" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态栏" >
          <el-select v-model="filters.state" placeholder="根据状态查询" clearable>
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getdepts">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd()">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="depts" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="部门编号" width="140" sortable>
      </el-table-column>
      <el-table-column prop="name" label="部门名称" width="140"  sortable>
      </el-table-column>
      <el-table-column prop="state" label="是否启用" width="140" :formatter="formatSex" sortable>
        <template slot-scope="scope" >
          <div slot="reference" class="name-wrapper" v-if="scope.row.state ==0" >
            <el-tag size="medium" type="danger">禁用</el-tag>
          </div>
          <div slot="reference" class="name-wrapper" v-if="scope.row.state ==1">
            <el-tag size="medium" type="success">启用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="employee.username" label="管理员" width="140" sortable>
      </el-table-column>
      <el-table-column prop="parent.name" label="上级部门" min-width="140" sortable>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handlesave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="部门编号" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="管理员">
          <!--
            将自定义的 HTML 模板插入el-option的 slot 中即可。
            placeholder:  占位符
            label:  选项的标签，若不设置则默认与 value 相同
        -->
          <el-select v-model="saveForm.manager_id" placeholder="请选择">
            <el-option
                v-for="item in employee"
                :key="item.id"
                :label="item.username"
                :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <!--<el-form-item label="上级部门">
          <el-cascader :options="options" :show-all-levels="false"></el-cascader>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      //关键子查询
      filters: {
        keyword: '',
        state:'',
        sn:'',
      },
      //部门表
      depts: [],
      //员工表
      employee: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      listLoading: false,//展示界面忙等
      sels: [],//列表选中列

      saveFormVisible: false,//编辑界面是否显示
      saveTitle: "",
      saveLoading: false,//修改保存的忙等
      saveFormRules: {
        name: [
            /*
              blur	当 input 失去焦点时触发	(event: Event)
              focus	当 input 获得焦点时触发	(event: Event)
            */
          { required: true, message: '请输入名称', trigger: 'blur'  },
          // { required: true, message: '请输入名称', trigger: 'focus'  }
        ],
        sn: [
          { required: true, message: '请输入部门编号', trigger: 'blur' }
        ],
      },
      //编辑界面数据
      saveForm: {
        id:'',
        sn: '',
        name: '',
        state: 0,
        manager_id: null,
        parent_id: null
      },

      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //新增界面数据
      addForm: {
        id:'',
        sn: '',
        name: '',
        state: 0,
        manager_id: null,
        parent_id: null
      }
    }
  },
  methods: {
    //性别显示转换
    formatSex: function (row, column) {
      return row.state == -1 ? '禁用' : row.state == 0 ? '启用' : '未知';
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getdepts();
    },
    handleSizeChange(val){
      this.pageSize = val
      this.getdepts();
    },

    //获取用户列表
    getdepts() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        //-------------------------关键字查询-------------------------------------
        keyword: this.filters.keyword,
        sn: this.filters.sn,
        state: this.filters.state
        // name: this.filters.name
      };

      //开启忙等
      this.listLoading = true;
      //获取部门列表分页的数据
      this.$http.post("/dept/queryList",para).then(res=>{
        this.depts = res.data.date
        this.total = res.data.total
        //关闭忙等
        this.listLoading = false;
      })
    },

    //获取员工列表
    getEmployee(){
      this.$http.get("/emp/list").then(res=>{
        console.log(res.data)
        this.employee = res.data
      })
    },

    //提取
    getByCurrentPage(){
      this.currentPage = 1;
      this.getdepts()
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '老子告诉你要谨慎删除', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.delete("/dept/"+row.id).then(res=>{
          this.listLoading = false;
          //NProgress.done();
          if(res.data.success){
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getdepts();
          }else{
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      }).catch(() => {
      //点取消之后的事
      });
    },

    //显示编辑界面
    handlesave: function (index, row) {
      this.saveTitle = "编辑";
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
    },

    //显示新增界面
    handleAdd: function () {
      this.saveTitle = "新增";
      this.saveForm = {
        id:'',
        sn: '',
        name: '',
        state: 1,
        manager_id: null,
        parent_id: null
      };
      this.saveFormVisible = true;
    },

    //编辑(新增+修改)
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {//手动触发验证功能
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;//开启忙等
            // let para = Object.assign({}, this.saveForm);//克隆
            let para = this.saveForm//直接赋值
            this.$http.put("/dept",para).then(res=>{
              if(res.data.success){
                this.saveLoading = false;
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                this.$refs['saveForm'].resetFields();//重置表单验证
                this.saveLoading = false;//关闭忙等
                this.getdepts();//添加成功刷新界面
              }else{
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                });
                this.saveFormVisible = false;
              }
            })
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        //忙等开启
        this.listLoading = true;
        this.$http.patch("/dept/del",ids).then(res=>{
          if(res.data.success){
            //如果执行成功关闭忙等
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getdepts();
          }else{
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      }).catch(() => {

      });
    }
  },
  //钩子
  mounted() {
    this.getdepts();
    this.getEmployee();
  }
}
</script>

<style scoped>

</style>